#!/usr/bin/env node

const http = require('http'),
      fs = require('fs'),
      cheerio = require('cheerio');

http.createServer((req,res)=>{
  const file = fs.readFileSync('news.html','utf8');
  res.setHeader("Content-Type","text/json;charset=utf-8"); 
  let newsList=[];

  if(req.url==='/'){
    let $ = cheerio.load(file);
    $('body').find('.news-model').each(function(){
      let newsTitle = $(this).find('b').text();

      if(newsTitle.charAt(3)=='8'){
        let creTime = $(this).find('span').text();
        let createTime = creTime.slice(1,creTime.length-1);
        const news = {
          "newsTitle":newsTitle,
          "createTime":createTime
        }
        newsList.push(news);
      }
    })
    res.end(JSON.stringify(newsList));
  }
}).listen(8080);
